//后台路由后台路由
var path = require('path');
var config = require("../config");
var ueditor = require("ueditor");
var Admin = require('../app/controllers/admin')
var Index = require('../app/controllers/index')
var Cases = require('../app/controllers/cases')
var Article = require('../app/controllers/article')
var banner = require('../app/controllers/banner')
var category = require('../app/controllers/category')
var Navigator = require('../app/controllers/navigator')
var Message = require('../app/controllers/message')
var Download = require('../app/controllers/download')
var friendlylink = require('../app/controllers/friendlylinks')
var Page = require('../app/controllers/page')
var Setup = require('../app/controllers/setup')
var Comment = require('../app/controllers/comment')
var _Setup = require("../app/models/setup");
var _Link = require("../app/models/friendlylinks");
var _nav = require("../app/controllers/navigator");
var Partners = require("../app/controllers/_partners");
module.exports = function (app) {
    //全局
    app.use(function (req, res, next) {
        _Setup.findOne({"_id":"sys"},function (err, sys) {
            if (err) {
                console.log(err)
            } else {
                _nav.getAllnav(function (err, navigator) {
                    if (err) {
                        console.log(err)
                    } else {
                        _Link.find({},function (err, link) {
                            app.locals.links = link;
                            app.locals.$sys = sys;
                            app.locals.navigator = navigator;
                            next();
                        })

                    }
                })
            }
        })
    })
	//ueditor编辑器上传图片
	app.use("/ueditor/ue",ueditor(path.join(__dirname, config.debug?"../public/":"../dist/"), function(req, res, next) {
		if(req.query.action === 'uploadimage'){
			// 这里你可以获得上传图片的信息
			var foo = req.ueditor;
			var img_url = "/attachment/";
			res.ue_up(img_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
		}
		//  客户端发起图片列表请求
		else if (req.query.action === 'listimage'){
			var dir_url = "/attachment/"; // 要展示给客户端的文件夹路径
			res.ue_list(dir_url) // 客户端会列出 dir_url 目录下的所有图片
		}else{
			res.setHeader('Content-Type', 'application/json');
			res.redirect('/ueditor/config.json')
		}
	}));

	/******前端*****/
	app.get('/ifname', Index.ifname);
	// 课程分类
	app.get('/courses', Index.courses);
    app.get('/courses/detail', Index.coursesShow);
	// 企业新闻
	app.get(['/news','/news/page/:num'], Index.news);
	app.get('/news/detail', Index.newsDetail);
	// 联系我们
	app.get('/contactus', Index.contactus);
    app.get('/abouts', Index.aboutus);
	app.post('/message', Message.save);
    app.get(['/search','/search/page/:num'], Index.search);
	// User
	app.post('/user/signin', Admin.login);
	app.get('/admin/login', Admin.showSignin);
	app.get('/admin/quit', Admin.quit);

	/******后台*****/
	app.get('/admin/main', Admin.isLogin, Admin.main);
	app.post('/admin/baseDelete',Admin.isLogin, Admin.baseDelete);
	
	//全局设置
	app.get('/admin/setupsys', Admin.isLogin, Setup.setupsys);
	app.get('/admin/setupseo', Admin.isLogin, Setup.setupseo);
	app.get('/admin/setuppassword', Admin.isLogin, Setup.setuppassword);
	app.post('/admin/editPassword', Admin.isLogin, Admin.editPassword);
	app.post('/admin/sysSave', Admin.isLogin, Setup.sysSave);
	
	//导航管理
	app.get('/admin/navigatorList', Admin.isLogin, Navigator.list);
	app.get('/admin/navigatorAdd', Admin.isLogin, Navigator.add);
	app.post('/admin/navigatorSave', Admin.isLogin, Navigator.save);
	app.get("/admin/navigatorUpdate/:id", Admin.isLogin, Navigator.update);
	app.get("/admin/navigatorDelete/:id", Admin.isLogin, Navigator.delete);
	
	//首页管理
	app.get("/admin/bannerList", Admin.isLogin, banner.bannerList);
	app.get("/admin/bannerAdd", Admin.isLogin, banner.bannerAdd);
	app.post("/admin/bannerSave", Admin.isLogin, banner.bannerSave);
	app.get("/admin/bannerUpdate/:id", Admin.isLogin, banner.bannerUpdate);
	app.get("/admin/bannerDelete/:id", Admin.isLogin, banner.bannerDelete);

	//合作伙伴
    app.get("/admin/partnersList", Admin.isLogin, Partners.partnersList);
    app.get("/admin/partnersAdd", Admin.isLogin, Partners.partnersAdd);
    app.post("/admin/partnersSave", Admin.isLogin, Partners.partnersSave);
    app.get("/admin/partnersUpdate/:id", Admin.isLogin, Partners.partnersUpdate);
    app.get("/admin/partnersDelete/:id", Admin.isLogin, Partners.partnersDelete);

	//新闻管理
	app.get('/admin/articleList', Admin.isLogin, Article.list);
	app.get('/admin/articleAdd', Admin.isLogin, Article.add);
	app.post('/admin/articleSave', Admin.isLogin, Article.save);
	app.get('/admin/articleEdit/:id', Admin.isLogin, Article.update);
	app.get('/admin/articleDelete/:id', Admin.isLogin, Article.delete);
	
	//课程管理
	app.get(['/admin/casesList','/admin/casesList/page/:num'], Admin.isLogin, Cases.list);
	app.get('/admin/casesAdd', Admin.isLogin, Cases.add);
	app.post('/admin/casesSave', Admin.isLogin, Cases.save);
	app.get('/admin/casesEdit/:id', Admin.isLogin, Cases.update);
	app.get('/admin/casesDelete/:id', Admin.isLogin, Cases.delete);

	//资料下载
	app.get(['/admin/downloadList','/admin/downloadList/page/:num'], Admin.isLogin, Download.list);
	app.get('/admin/downloadAdd', Admin.isLogin, Download.add);
	app.post('/admin/downloadSave', Admin.isLogin, Download.save);
	app.get('/admin/downloadEdit/:id', Admin.isLogin, Download.update);
	app.get('/admin/downloaDelete/:id', Admin.isLogin, Download.delete);
	app.get(['/download','/download/page/:num'], Index.download);
	//评价管理
    app.get(['/admin/commentList','/admin/commentList/page/:num'], Admin.isLogin, Comment.list);
    app.get('/admin/commentAdd', Admin.isLogin, Comment.add);
    app.post('/admin/commentSave', Admin.isLogin, Comment.save);
    app.get('/admin/commentEdit/:id', Admin.isLogin, Comment.update);
    app.get('/admin/commentDelete/:id', Admin.isLogin, Comment.delete);


	//分类管理
	app.get(['/admin/categoryList', '/admin/categoryList/page/:num'], Admin.isLogin, category.zhengshuList);
	app.get(['/admin/categoryNewsList', '/admin/categoryNewsList/page/:num'], Admin.isLogin, category.newList);
	app.get('/admin/categoryListDelete', Admin.isLogin, category.delete);
	app.post('/admin/categoryListSave', Admin.isLogin, category.save);
	
	//友情连接
	app.get(['/admin/friendlylinkList', '/admin/friendlylinkList/page/:num'], Admin.isLogin, friendlylink.list);
	app.post('/admin/friendlylinkListSave', Admin.isLogin, friendlylink.save);
	app.get('/admin/friendlylinkListDelete', Admin.isLogin, friendlylink.delete);
	
	//其他管理
	app.get(['/admin/pageList', '/admin/pageList/page/:num'], Admin.isLogin, Page.list);
	app.get('/admin/messageList', Admin.isLogin, Message.list);
    app.get('/admin/registrationList', Admin.isLogin, Message.list2);
	app.get('/admin/messageDetails/:id', Admin.isLogin, Message.details);
	app.get('/admin/messageDelete/:id', Admin.isLogin, Message.delete);
	app.get('/admin/pageListAdd', Admin.isLogin, Page.add);
	app.post('/admin/pageListSave', Admin.isLogin, Page.save);
	app.get('/admin/pageListEdit/:alias', Admin.isLogin, Page.update);
	app.get('/admin/pageListDelete/:alias', Admin.isLogin, Page.delete);
}